package com.kanshu.client.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.kanshu.client.R;
import com.kanshu.client.util.DeviceUtils;


public class CommonDialog extends Dialog {

    public static final int POSITIVE_BUTTON = 0x001;
    public static final int NEGATIVE_BUTTON = 0x002;


    public CommonDialog(@NonNull Context context) {
        super(context, R.style.Dialog);
    }


    public static class Builder {
        private static final String DEFAULT_TITLE = "提示";
        private static final String DEFAULT_POSITIVE_TEXT = "确定";
        private static final String DEFAULT_NEGATIVE_TEXT = "取消";
        //标题
        private String title;
        //提示信息
        private String contentText;
        //确定按钮文本
        private String positiveText;
        //否定按钮文本
        private String negativeText;
        //确定按钮监听
        private OnClickListener positiveListener;
        //否定按钮监听
        private OnClickListener negativeListener;


        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentText(String contentText) {
            this.contentText = contentText;
            return this;
        }

        public Builder setPositiveText(String positiveText) {
            this.positiveText = positiveText;
            return this;
        }

        public Builder setNegativeText(String negativeText) {
            this.negativeText = negativeText;
            return this;
        }

        public Builder setPositiveListener(OnClickListener positiveListener) {
            this.positiveListener = positiveListener;
            return this;
        }

        public Builder setNegativeListener(OnClickListener negativeListener) {
            this.negativeListener = negativeListener;
            return this;
        }


        public CommonDialog create(Context context) {
            final CommonDialog dialog = new CommonDialog(context);
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_common, null);
            TextView tvTitle = view.findViewById(R.id.tv_title);
            TextView tvContent = view.findViewById(R.id.tv_content);
            Button btnPositive = view.findViewById(R.id.btn_positive);
            Button btnNegative = view.findViewById(R.id.btn_negative);
            //设置标题
            tvTitle.setText(TextUtils.isEmpty(title) ? DEFAULT_TITLE : title);
            //设置内容提示
            if (TextUtils.isEmpty(contentText)) {
                throw new RuntimeException("对话框提示信息不能为空");
            }
            tvContent.setText(Html.fromHtml(contentText));
            //确定按钮
            if (positiveListener == null) {
                btnPositive.setVisibility(View.GONE);
            } else {
                btnPositive.setText(TextUtils.isEmpty(positiveText) ? DEFAULT_POSITIVE_TEXT : positiveText);
                btnPositive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        positiveListener.onClick(dialog, POSITIVE_BUTTON);
                    }
                });
            }
            //否定按钮
            if (negativeListener == null) {
                btnNegative.setVisibility(View.GONE);
            } else {
                btnNegative.setText(TextUtils.isEmpty(negativeText) ? DEFAULT_NEGATIVE_TEXT : negativeText);
                btnPositive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        negativeListener.onClick(dialog, NEGATIVE_BUTTON);
                    }
                });
            }

            dialog.setContentView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            Window window = dialog.getWindow();
            if (window != null) {
                int[] sceenSize = DeviceUtils.getScreenSizePixels(context);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = (int) (sceenSize[0] * 0.7);
                lp.height = (int) (sceenSize[1] * 0.6);
                window.setAttributes(lp);
            }
            dialog.setCanceledOnTouchOutside(false);
            return dialog;
        }


    }

}
